<?php class MClient extends CI_Model {

	function __construct()
	    {
		parent::__construct();
	    }

	//récupère tous les membres de la table membre
	function getAllClient() {
		$data = array();
		$Q = $this->db->get('membre');
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
			$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}

	function getAllClientPrivilege() {
		$data = array();
		$sql = "SELECT * FROM client as C, privilege as P WHERE C.id_privilege = P.id_privilege ";
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
			$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getAllClientPrivilege1() {
		$data = array();
		$sql = "SELECT * FROM client as C, privilege as P WHERE C.id_privilege = P.id_privilege";
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
			$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	
	
	function insertClient($data) {		
		$civilite = $data['civilite'];
		$isFacebook = $data['isFacebook'];
		$nom = $data['nom'];
		$prenom = $data['prenom'];
		$email = $data['email'];
		$adresse = $data['adresse'];
		$cp = $data['cp'];
		$ville = $data['ville'];
		$mdp = $data['mdp'];
		$login = $data['login'];
		$pays = $data['pays'];
		$nom_adresse = $data['adresse'];
		$latitude = 'L';
		$longitude = 'l';
		
		$sql1 ="INSERT INTO adresse (cp, latitude, longitude, nom_adresse, ville, pays) VALUES ('$cp', '$latitude', '$longitude', '$nom_adresse', '$ville', '$pays')";
		$sql2 = "INSERT INTO email (email) VALUES ('$email')";
		$sql3 = "INSERT INTO client (id_client,civilite, nom, prenom, login, mdp, id_email, id_adresse, id_privilege, isFacebook) VALUES ('', '$civilite', '$nom', '$prenom', '$login', '$mdp', (SELECT id_email FROM email WHERE email='$email') , (SELECT id_adresse FROM adresse WHERE nom_adresse='$nom_adresse'), 2, '$isFacebook')";
		$this->db->query($sql1);
		$this->db->query($sql2);
		$this->db->query($sql3);		
	}
	
	
	
	
	function getInfoClient($login) {
		$sql = "SELECT * FROM client as C, privilege as P WHERE C.login = ? AND C.id_privilege = P.id_privilege";			
		$Q = $this->db->query($sql, array($login));
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getInfoClientEmail($email){
		$sql1 = "SELECT id_email FROM email WHERE email = '$email' ";
		$id_email = 0;
		$Q = $this->db->query($sql1);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$id_email=$row['id_email'];
			}
		}
		$sql = "SELECT * FROM client AS C, privilege AS P, email AS E, adresse AS A WHERE C.id_email=$id_email AND C.id_privilege = P.id_privilege AND C.id_email = E.id_email AND C.id_adresse = A.id_adresse";				
		$data = array();	
		$Q = $this->db->query($sql, array($email));
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function isPro($id_client){
		$sql1 = "SELECT isSociete FROM client WHERE id_client = '$id_client' ";
		$Q = $this->db->query($sql1);
		$id = 0;
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$id=$row['isSociete'];
			}
		}
		if($id==0){
			return false;
		}
		else {
			return true;
		}
		
	}
	
	function getInfoClientLogin($login){		
		$sql = "SELECT * FROM user WHERE login= '$login' ";
		$data = array();	
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getInfoClientId($id_client){		
		$sql = "SELECT * FROM client AS C, email AS E, adresse AS A WHERE C.id_client= ? AND C.id_email = E.id_email AND C.id_adresse = A.id_adresse";
		//
		$data = array();	
		$Q = $this->db->query($sql, array($id_client));
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function updateMdp($login, $mdp) {
	    $data = array(
		'login' => $login, 
		'mdp' => $mdp
	    );
	    $this->db->where('login', $login);
	    $this->db->update('client', $data);
	} 
	
	function getInfoClientTout($id_client){		
		$sql = "SELECT * FROM client AS C, email AS E, adresse AS A WHERE C.id_client= ? AND C.id_email = E.id_email AND C.id_adresse = A.id_adresse";				
		//$sql = "SELECT * FROM client where id_client= ?";
		$data = array();	
		$Q = $this->db->query($sql, array($id_client));
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function updateisConnect($id_client, $statusconn) {
	    $data = array(
	        'isConnect' => $statusconn                      
	    );
	    $this->db->where('id_client', $id_client);
	    $this->db->update('client', $data);
	}   
	
	function getAllClientConnecte() {
		$data = array();
		$sql = "SELECT * FROM client as C, privilege as P, email as E WHERE C.id_privilege = P.id_privilege AND C.id_email = E.id_email AND C.isConnect=1";
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
			$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	
	function loginAdmin($login, $pass) {
		$sql = "SELECT * FROM client as C, privilege as P WHERE C.login = ? AND C.mdp = ? AND C.id_privilege=1 AND C.id_privilege = P.id_privilege";		
		$Q = $this->db->query($sql, array($login, $pass));
		if ($Q->num_rows()!=null){
			return true;
		}
		else {
			return false;
		}
		$Q->free_result();
	}	
}
?>